﻿using System.Windows.Media;
using System.Windows;

namespace Super.Hmi.Utils
{
    public static class PositionHelper
    {
        /// <summary>
        /// 获取元素相对于容器的位置
        /// </summary>
        /// <param name="element"></param>
        /// <param name="container"></param>
        /// <returns></returns>
        public static Point GetRelativePosition(FrameworkElement element, FrameworkElement container)
        {
            try
            {
                if (element == null || container == null)
                    return new Point(0, 0);

                // 计算相对于容器的位置
                GeneralTransform transform = element.TransformToVisual(container);
                return transform.Transform(new Point(0, element.ActualHeight));
            }
            catch
            {
                return new Point(0, 0);
            }
        }
        /// <summary>
        /// 获取元素在屏幕上的绝对位置
        /// </summary>
        /// <param name="element"></param>
        /// <returns></returns>
        public static Point GetScreenPosition(FrameworkElement element)
        {
            try
            {
                if (element == null)
                    return new Point(0, 0);

                // 获取屏幕绝对位置
                Point screenPoint = element.PointToScreen(new Point(0, element.ActualHeight));

                // 转换为相对坐标
                PresentationSource source = PresentationSource.FromVisual(Application.Current.MainWindow);
                if (source?.CompositionTarget != null)
                {
                    return source.CompositionTarget.TransformFromDevice.Transform(screenPoint);
                }

                return screenPoint;
            }
            catch
            {
                return new Point(0, 0);
            }
        }
    }
}
